<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="dialogVisible = true" type="primary" size="mini">添加分类</el-button>
      </div>
      <el-table  border stripe  :data="cateList"  style="width: 100%">
      <el-table-column label="添加文章" type="index" width="100" prop="id">
      </el-table-column>
      <!-- 添加分类的 -->
      <el-table-column  prop="cate_name"  label="分类名称">
      </el-table-column>
      <el-table-column  prop="cate_alias"  label="分类别名">
      </el-table-column>
      <el-table-column  label="操作">
        <template v-slot="scope">
          <el-button type="primary" size="mini" @click="showEdit(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="delCate(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <!-- 添加分类的dialog -->
      <el-dialog
        @close="addClose"
        title="添加文章分类"
        :close-on-click-modal="false"
        :visible.sync="dialogVisible"
        width="35%"
        :before-close="handleClose">
        <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="100px" class="demo-addForm">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" >取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog
        @close="$refs.editForm.resetFields()"
        title="修改分类"
        :close-on-click-modal="false"
        :visible.sync="editVisible"
        width="35%"
        :before-close="handleClose">
        <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="100px" >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="danger" size="mini"  @click="editVisible = false">取消</el-button>
          <el-button type="primary" size="mini" @click="editCate">确定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'ArtCate',
  data () {
    return {
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 分类列表数据
      cateList: [],
      dialogVisible: false,
      editVisible: false,
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]

      }

    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取分类列表数据
    async  getCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return
      // console.log(res.data)
      this.cateList = res.data
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    addClose () {
      this.$refs.addForm.resetFields()
    },
    // 隐藏dialog
    add () {
      // 1.校验表单
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 3.关闭dialog
        this.dialogVisible = false
        // 4.重新获取分类列表数据
        this.getCateList()
      })
    },
    showEdit (row) {
      // if (row.id === 1 || row.id === 2) return this.$message.error('该分类不允许修改')
      this.editVisible = true
      // 将当前行数据赋值给editForm,对线的浅拷贝
      this.editForm = { ...row }
    },
    editCate () {
      // 1.校验表单
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // 3根据结果提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        // 4.关闭dialog
        this.editVisible = false
        // 5.重新获取分类列表数据
        this.getCateList()
      })
    },
    // delCate (id) {
    //   // 1.事件传递id
    //   // 2.点击先提示用户是否删除
    //   this.$confirm('确定删除该条信息吗, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(async () => {
    //     const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
    //     this.$message({ type: 'success', message: '删除成功!' })
    //     if (res.code !== 0) return this.$message.error(res.message)
    //     this.$message.success(res.message)
    //     // 重新渲染
    //     this.getCateList()
    //   }).catch(() => {
    //   })
    // }
    async delCate (id) {
      // 1.事件传递id
      // 2.点击先提示用户是否删除
      const result = this.$confirm('确定删除该条信息吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(e => e)
      if (result !== 'confirm') return
      // 3.发送请求
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      // 4.根据结果提醒用户
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 重新渲染
      this.getCateList()
    }
  }
}

</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
